package com.crm.jdy.controller.usermanager;

import com.crm.jdy.common.dto.UserInfoDto;
import com.crm.jdy.common.myexception.MyException;
import com.crm.jdy.common.pojo.UserInfo;
import com.crm.jdy.common.utils.MD5Utils;
import com.crm.jdy.common.utils.api.ResultVo;
import com.crm.jdy.common.utils.currentlimiting.RateLimit;
import com.crm.jdy.service.usermanager.UserInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import javax.validation.constraints.NotNull;

/**
*
* <p>说明： 用户管理API接口层</P>
* @version: V1.0
* @author: kong
* @time    2021年3月30日
*
*/
@Api(tags = "userInfo", description = "用户管理")
@RestController
@RequestMapping("/userInfo")
public class UserInfoController{

    @Reference(version = "${service.version}", check = false)
    UserInfoService callAplanInfoService;

    @RateLimit()
    @ApiOperation("用户管理信息")
    @PostMapping("/seleteUserInfo")
    public ResultVo seleteUserInfo( @RequestBody UserInfoDto callAplanInfoDto){
        PageInfo<UserInfo> list = callAplanInfoService.seleteUserInfo(callAplanInfoDto);
        return  ResultVo.success(list);
    }

    @RateLimit()
    @ApiOperation("更新用户管理")
    @PostMapping("/updatUserInfo")
    public ResultVo updatUserInfo(@ApiParam(value = "userInfo", required = true)
                                   @NotNull  @RequestBody UserInfo userInfo){
        if(ObjectUtils.isEmpty(userInfo) && null != userInfo.getUserPwd() ){
            userInfo.setUserPwd(MD5Utils.inputPassToFormPass(userInfo.getUserPwd()));
        }
        boolean index = callAplanInfoService.updateUserInfo(userInfo);
        if(index){
            return  ResultVo.success();
        }
        return  ResultVo.failed();
    }

    @RateLimit()
    @ApiOperation("添加用户管理")
    @PostMapping("/addUserInfo")
    public ResultVo addUserInfo(@ApiParam(name = "callAplanInfo", required = false)
                                @Validated  @RequestBody UserInfo callAplanInfo) throws MyException {
        callAplanInfo.setUserPwd(MD5Utils.inputPassToFormPass(callAplanInfo.getUserPwd()));
        boolean list  = callAplanInfoService.addUserInfo(callAplanInfo);
        if(list){
            return ResultVo.success();
        }
        return ResultVo.failed();
    }

    @RateLimit()
    @ApiOperation("删除用户管理")
    @DeleteMapping("deleteUserInfo/{userId}")
    public ResultVo deleteUserInfo(@NotNull @ApiParam(name = "userId", value = "userId", required = true) @PathVariable String userId){
        boolean b = callAplanInfoService.removeUserInfo(userId);
        if(b){
            return ResultVo.success();
        }
        return ResultVo.failed();
    }


}